package com.imooc.dao;

import com.imooc.bean.Message;
import com.imooc.db.DBAccess;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.math.BigInteger;
import java.sql.*;
import java.time.Instant;
import java.util.*;
import java.util.Date;

/**
 * 和message表相关的数据库操作
 */
public class MessageDao {
    /**
     * 根据查询条件查询消息列表(MyBatis)
     */
    public List<Message> queryMessage(String command, String description) {
        DBAccess dbAccess = new DBAccess();
        List<Message> messageList = new ArrayList<Message>();
        // SqlSession sqlSession = null;
        // try with resource
        try (SqlSession sqlSession = dbAccess.getSqlSession()) {
            // 通过SqlSession 执行SQL语句
            Message message = new Message();
            message.setCommand(command);
            message.setDescription(description);
            // 传统方法
            //messageList = sqlSession.selectList("Message.queryMessageList", message);
            // 接口式编程, IMessage是代理实例. (动态代理)
            IMessage iMessage = sqlSession.getMapper(IMessage.class);
            messageList = iMessage.queryMessageList(message);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return messageList;
    }

    /**
     *  根据查询条件查询消息列表的条数
     */
    public int count(Message message) {
        DBAccess dbAccess = new DBAccess();
        int result = 0;
        try(SqlSession sqlSession = dbAccess.getSqlSession()) {

            IMessage iMessage = sqlSession.getMapper(IMessage.class);
            result = iMessage.count(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  result;
    }

    /**
     * 根据查询条件分页查询消息列表
     */
    public List<Message> queryMessageListByPage(Map<String ,Object> parameter)  {
        List<Message> list = new ArrayList<Message>();

        DBAccess dbAccess = new DBAccess();

        try(SqlSession sqlSession = dbAccess.getSqlSession()) {
            IMessage iMessage = sqlSession.getMapper(IMessage.class );
            list = iMessage.queryMessageListByPage(parameter);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }


    /**
     * 单条数据删除
     */
    public void deleteOne(int id) {
        DBAccess dbAccess = new DBAccess();
        // SqlSession sqlSession = null;
        try (SqlSession sqlSession = dbAccess.getSqlSession()) {
            // 通过SqlSession 执行SQL语句
            int result = sqlSession.delete("Message.deleteOne", id);
            // 提交事务
            sqlSession.commit();
            System.out.println("删除结果: " + result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 数据批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        DBAccess dbAccess = new DBAccess();
        try (SqlSession sqlSession = dbAccess.getSqlSession()) {
            sqlSession.delete("Message.deleteBatch", ids);
            sqlSession.commit();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        BigInteger a  = BigInteger.probablePrime(6, new Random());
        BigInteger bigInteger = BigInteger.probablePrime(7,new Random());
//        java.util.EnumSet
        try {
            System.out.println(specialUrlEncode("+123*%7E"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static String specialUrlEncode(String value) throws Exception {
        return java.net.URLEncoder.encode(value, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
    }
    /**
     * 根据查询条件查询消息列表(JDBC)
     */
    /*public List<Message> queryCommand(String command,String description) {
        List<Message> messagesList = new ArrayList<Message>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnectionc("jdbc:mysql://118.24.117.178:3306/testDB", "testDB", "testDB");
            StringBuilder sql = new StringBuilder("SELECT ID,COMMAND,DESCRIPTION,CONTENT FROM message WHERE 1=1");

            List<String> paramList = new ArrayList<String>();
            if (command != null && !"".equals(command.trim())) {
                sql.append(" and COMMAND = ?");
                paramList.add(command);
            }
            if (description != null && !"".equals(description.trim())) {
                sql.append(" and DESCRIPTION like '%' ? '%' ");
                paramList.add(description);
            }
            PreparedStatement preparedStatement = conn.prepareStatement(sql.toString());
            for (int i = 0; i < paramList.size(); i++) {
                preparedStatement.setString(i + 1, paramList.get(i));
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            System.out.println("SQL: " + sql);
            while (resultSet.next()) {
                Message message = new Message();
                messagesList.add(message);
                message.setId(resultSet.getString("ID"));
                message.setCommand(resultSet.getString("COMMAND"));
                message.setDescription(resultSet.getString("DESCRIPTION"));
                message.setContent(resultSet.getString("CONTENT"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
            return messagesList;
    }*/
}